const {Invalid,Modified,Shared} = require('./State')
const { env } = require('process')

class CacheBlock{
    /**
     * @param {number} processor
     * 隶属的处理器编号
     * @param {number} id
     * 缓存块编号
     */
    constructor(processor,id){
        this.id = id
        this.processor = processor
        this.content = -1 // 缓存块映射的内存编号
        this.states = {
            Invalid : new Invalid(this),
            Modified : new Modified(this),
            Shared : new Shared(this),
        }
        this.currentState = null
        this.changeState('Invalid')
    }

    changeState(state){
        this.currentState = this.states[state]
        this.currentState.show()
    }

    changeContent(content){
        this.content = content
    }

    /**
     * 处理器读
     */
    prRd(){
        this.currentState.prRd()
    }

    /**
     * 处理器写
     */
    prWr(){
        this.currentState.prWr()
    }

    /**
     * 总线读
     */
    busRd(){
        this.currentState.busRd()
    }

    /**
     * 总线读写
     */
    busRdX(){
        this.currentState.busRdX()
    }

    /**
     * 写回内存
     */
    flush(){
        this.currentState.flush()
    }
}
module.exports = CacheBlock